﻿using System;
using System.Collections.Generic;
using Soveryn.Commerce.BaseClasses;
using Soveryn.Commerce.Shared;

namespace Soveryn.Commerce.Doodads
{

    public partial class Contact : DoodadBaseClass
    {

        public string InlineEditID;

        new protected void Page_Load(object sender, EventArgs e)
        {

            base.Page_Load(sender, e);

            if (IsPostBack && Request.Params["__EVENTTARGET"] == "ContactForm")
            {
                String data = Request.Params["__EVENTARGUMENT"];
                String[] fields = data.Split(',');
                String html = "<table>";
                foreach (String field in fields)
                {

                    if (String.IsNullOrEmpty(field))
                        continue;

                    String[] parts = field.Split('|');
                    String fieldName = parts[0];
                    String fieldData = Soveryn.Commerce.Shared.StringUtils.DecodeFrom64(parts[1]);
                    html += "<tr>";
                    html += "<td>" + fieldName + ":</td>";
                    html += "<td><strong>" + fieldData + "</strong></td>";
                    html += "</tr>";
                }
                html += "</table>";

                APIEnvelope env = Soveryn.Commerce.Shared.API.Call(PageBaseClass.WebsiteAPIURL, "SendEmail", new
                {
                    WebsiteAPIKey = PageBaseClass.WebsiteAPIKey,
                    strFrom = ParentPage.Website.OrderManagementEmail,
                    strTo = ParentPage.Website.OrderManagementEmail,
                    strSubject = "Contact us",
                    strBody = html,
                    strBcc = "",
                    isHTML = true
                }, true, false);

                ParentPage.ExecuteScript("alert('Thank you!');");



            }

            if (IsPostBack)
                return;

            HTMLLiteral.Text = Properties.DoodadInstanceHTML;
            InlineEditID = "inlineEdit_" + this.Properties.DoodadInstanceID;

            if (ParentPage.EditMode && ParentPage.EmployeeLoggedIn)
            {
                HTMLPanel.CssClass = "editable";
                HTMLPanel.Attributes.Add("data-id", Properties.ElementID);
            }

        }

    }
}
